package spring.rabbitmq.workqueues;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author ZCW
 * @createTime 2018/4/29
 * @lastUpdateTime 2018/4/29 ZCW
 */
public class Producer {

    // 该接口定义了一套针对AMQP协议的基础操作
    @Autowired
    AmqpTemplate amqpTemplate;

    @Autowired
    Queue queue;

    int dots = 0;
    int count = 0;

    /**
     * 使用 . 的个数模拟业务的复杂度
     * <p>
     * 0.5 秒后开始，每秒发送一条消息
     *
     * @param
     * @return: void
     */
    @Scheduled(fixedDelay = 1000, initialDelay = 500)
    public void send() {

        StringBuilder builder = new StringBuilder("Hello");
        if (dots++ == 3) {
            dots = 1;
        }
        for (int i = 0; i < dots; i++) {
            builder.append('.');
        }

        builder.append(Integer.toString(++count));
        String message = builder.toString();
        amqpTemplate.convertAndSend(queue.getName(), message);
        System.out.println(" [x] Sent '" + message + "'");
    }
}
